package com.example.demo.controller;

import com.example.demo.Util.Modbus4jUtils;
import com.example.demo.Util.Modbus4jWriteUtils;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @author liuningning
 * @date 2024/12/25 13:10
 */
@RestController
@RequestMapping("/modbus")
public class Modbus4jController {

    @Resource
    Modbus4jWriteUtils modbus4jWriteUtils;

    @GetMapping("/readOne")
    public String readOne() throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        Modbus4jUtils modbus4jUtils = new Modbus4jUtils("127.0.0.1", 502);
        System.out.println(modbus4jUtils.readCoilStatus(1, 0));
        System.out.println(modbus4jUtils.readCoilStatus(1, 1));
        System.out.println(modbus4jUtils.readCoilStatus(1, 2));
        System.out.println(modbus4jUtils.readCoilStatus(1, 3));
        return "ok";
    }

    @GetMapping("/readTwo")
    public String readTwo() throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        Modbus4jUtils modbus4jUtils = new Modbus4jUtils("127.0.0.1", 502);
        System.out.println(modbus4jUtils.readInputStatus(1, 0));
        System.out.println(modbus4jUtils.readInputStatus(1, 1));
        System.out.println(modbus4jUtils.readInputStatus(1, 2));
        System.out.println(modbus4jUtils.readInputStatus(1, 3));
        return "ok";
    }

    @GetMapping("/readThree")
    public String readThree() throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        Modbus4jUtils modbus4jUtils = new Modbus4jUtils("10.5.125.245", 502);
//        System.out.println(modbus4jUtils.readHoldingRegister(40031, 0, DataType.TWO_BYTE_INT_SIGNED));
        System.out.println(modbus4jUtils.readHoldingRegister(1, 31, DataType.TWO_BYTE_BCD));
//        System.out.println(modbus4jUtils.readHoldingRegister(40031, 2, DataType.TWO_BYTE_INT_SIGNED));
//        System.out.println(modbus4jUtils.readHoldingRegister(40031, 3, DataType.TWO_BYTE_INT_SIGNED));
        return "ok";
    }
    @GetMapping("/readFour")
    public String readFour() throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        Modbus4jUtils modbus4jUtils = new Modbus4jUtils("127.0.0.1", 502);
        System.out.println(modbus4jUtils.readInputRegisters(1, 0, DataType.FOUR_BYTE_FLOAT));
        System.out.println(modbus4jUtils.readInputRegisters(1, 1, DataType.FOUR_BYTE_FLOAT));
        System.out.println(modbus4jUtils.readInputRegisters(1, 2, DataType.FOUR_BYTE_FLOAT));
        System.out.println(modbus4jUtils.readInputRegisters(1, 3, DataType.FOUR_BYTE_FLOAT));
        return "ok";
    }

    @GetMapping("/writeOne")
    public String writeOne() throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        modbus4jWriteUtils.writeCoil(2, 9, true);
        return "ok";
    }


    @GetMapping("/writeThree")
    public Boolean writeThree() throws ModbusInitException, ModbusTransportException, ErrorResponseException {
        short v = 3;
        return modbus4jWriteUtils.writeRegister(1, 2, v);
    }

}
